package com.hyf.meterialdesign.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hyf.meterialdesign.R;
import com.hyf.meterialdesign.adapter.SimpleFragmentAdaptper;
import com.hyf.meterialdesign.base.BaseAppBarActivity;
import com.hyf.meterialdesign.fragment.SimpleFragment;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识点地址：http://www.jianshu.com/p/107a2529a56f
 */

public class AppBarLayoutActivity extends BaseAppBarActivity {

    private Toolbar toolbar;
    private CoordinatorLayout rootView;
    private TabLayout tab;
    private CollapsingToolbarLayout collapsingLayout;

    private ViewPager vpHeader,viewPager;
    private List<SimpleFragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_bar_layout);
        initView();
        initData();
    }

    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        rootView = (CoordinatorLayout) findViewById(R.id.activity_app_bar_layout);
        tab = (TabLayout) findViewById(R.id.tab);
        collapsingLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingLayout);
        vpHeader = (ViewPager) findViewById(R.id.vp_header);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    private void initData() {
//        toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        toolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        toolbar.inflateMenu(R.menu.menu_appbarlayout_activity);
        toolbar.setTitle("AppBarLayoutActivity");
        collapsingLayout.setTitle("AppBarLayoutActivity");
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.simple_snack:
                        Snackbar.make(rootView,"落霞与孤鹜齐飞",Snackbar.LENGTH_LONG).show();
                        break;
                    case R.id.snack_action:
                        Snackbar.make(rootView,"我爱朱晓慧",Snackbar.LENGTH_LONG).setAction("yes", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(AppBarLayoutActivity.this, "yes", Toast.LENGTH_SHORT).show();
                            }
                        }).show();
                        break;
                    case R.id.snack_color:
                        Snackbar snackbar = Snackbar.make(rootView, "我爱朱晓慧", Snackbar.LENGTH_LONG);
                        snackbar.setAction("yes", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(AppBarLayoutActivity.this, "YES", Toast.LENGTH_SHORT).show();
                            }
                        });
                        snackbar.setActionTextColor(Color.YELLOW);
                        setSnackBarColor(snackbar,Color.parseColor("#ff705e"),Color.parseColor("#eeeeee"));
                        snackbar.show();
                        break;
                }
                return true;
            }
        });
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("AppBarLayoutActivity");

        vpHeader.post(new Runnable() {
            @Override
            public void run() {
                initHeaderVp();
            }
        });
        initViewPager();
        initTab();
    }

    private void initHeaderVp() {
        //初始化头部的viewpager
        List<SimpleFragment> headerFragments = new ArrayList<>();
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554730265&di=6134e601b351ff254e77e4096412979d&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201508%2F08%2F20150808112252_MUhiH.jpeg",true));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554784000&di=5ce5441c25325032072b47b53cac051f&imgtype=0&src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201505%2F10%2F20150510001633_TmsQR.thumb.700_0.jpeg",true));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491554865529&di=4467b85ada68155e4f193595a7448a3f&imgtype=0&src=http%3A%2F%2Fimg3.duitang.com%2Fuploads%2Fitem%2F201605%2F08%2F20160508181552_WeKRX.jpeg",true));
        headerFragments.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491555017920&di=508ea534773780aef84bc36a59bc62b4&imgtype=0&src=http%3A%2F%2Fm.qqzhi.com%2Fupload%2Fimg_0_1342580691D3780971425_23.jpg",true));
        vpHeader.setAdapter(new SimpleFragmentAdaptper(getSupportFragmentManager(),headerFragments));
    }

    private void initTab() {
        tab.setSelectedTabIndicatorColor(Color.YELLOW);
//        tab.setTabTextColors(Color.WHITE,Color.YELLOW);
        tab.setupWithViewPager(viewPager);
        tab.setBackgroundColor(Color.parseColor("#2196F3"));
    }

    private void initViewPager() {
        //初始化viewpager
        fragmentList = new ArrayList<>();
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491542433336&di=f47f07aa289c9e828ac221dd38a99b41&imgtype=0&src=http%3A%2F%2Fbcs.91.com%2Fpcsuite-dev%2Fimg%2F0%2F480_800%2F57be51f2609a1e49c2364a99fbdeef0f.jpg",false));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491542433322&di=0d4077c6cde7b81798b3804db95d75d4&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fw%253D580%2Fsign%3D818776f83987e9504217f3642039531b%2Fb520f288d43f879493453115d11b0ef41bd53a33.jpg",false));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491542110913&di=e058ea8e227c730507c85c20caee5053&imgtype=0&src=http%3A%2F%2Fwww.bz55.com%2Fuploads%2Fallimg%2F150401%2F139-1504011I604-50.jpg",false));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491542110912&di=212743286395cd1bb1251ba2cb4e4cdf&imgtype=0&src=http%3A%2F%2Fc.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2Fa2cc7cd98d1001e923097a57be0e7bec54e797b4.jpg",false));
        fragmentList.add(SimpleFragment.newInstance("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491542110912&di=e3103f4a68326d4e993cdeea83e03913&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F024f78f0f736afc34a24c98eb119ebc4b7451234.jpg",false));
        String [] titles = new String[fragmentList.size()];
        for (int i = 0; i < fragmentList.size(); i++) {
            titles[i] = "TAB"+(i+1);
        }
        viewPager.setAdapter(new SimpleFragmentAdaptper(getSupportFragmentManager(), fragmentList,titles));
    }


    private void setSnackBarColor(Snackbar snackbar,int bgcolor,int msgColor){
        View view = snackbar.getView();
        view.setBackgroundColor(bgcolor);
        TextView textView = (TextView) view.findViewById(R.id.snackbar_text);
        textView.setTextColor(msgColor);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_appbarlayout_activity,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.simple_snack:
                Snackbar.make(rootView,"落霞与孤鹜齐飞",Snackbar.LENGTH_LONG).show();
                break;
            case R.id.snack_action:
                Snackbar.make(rootView,"我爱朱晓慧",Snackbar.LENGTH_LONG).setAction("yes", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(AppBarLayoutActivity.this, "yes", Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            case R.id.snack_color:
                Snackbar snackbar = Snackbar.make(rootView, "我爱朱晓慧", Snackbar.LENGTH_LONG);
                snackbar.setAction("yes", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(AppBarLayoutActivity.this, "YES", Toast.LENGTH_SHORT).show();
                    }
                });
                snackbar.setActionTextColor(Color.YELLOW);
                setSnackBarColor(snackbar,Color.parseColor("#ff705e"),Color.parseColor("#eeeeee"));
                snackbar.show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 通过反射，设置menu显示icon
     *
     * @param view
     * @param menu
     * @return
     */
    @Override
    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
        return super.onPrepareOptionsPanel(view, menu);
    }
}
